package ruifeng.zcj.dig.ui.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.view.View;

import java.util.ArrayList;

import butterknife.Bind;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.FragmentInfo;
import ruifeng.zcj.dig.base.ViewPagerFragmentAdapter;
import ruifeng.zcj.dig.ui.view.XViewPager;
import ruifeng.zcj.mybase.base.App;
import ruifeng.zcj.mybase.base.BaseFragment;

/**
 * Fragment中添加Fragment的父类
 * https://segmentfault.com/q/1010000006660213?_ea=1088093
 * Created by Administrator on 2016/9/23 0023.
 */
public abstract class DepositFragment extends BaseFragment {

    public static final String KEY_CLASS_NAME = "className";
    public static final String KEY_ARGS = "args";

    @Bind(R.id.view_pager)
    public XViewPager mViewPager;

    public ViewPagerFragmentAdapter mBaseFragmentViewPageAdapter;


   /* @Override  http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed
    public int getContentView() {
     java.lang.IllegalStateException: Activity has been destroyed
                                                                   at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
                                                                   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
                                                                   at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
                                                                   at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:476)
                                                                   at ruifeng.zcj.dig.ui.base.DepositFragment.initContainer(DepositFragment.java:63)
                                                                   at ruifeng.zcj.dig.ui.base.DepositFragment.initFragmentConfig(DepositFragment.java:46)
        return R.layout.xview_pager;
    }*/

    @Override
    protected void initFragmentConfig(View view) {
           /* FragmentManager fm = getChildFragmentManager();
            // tag 是你在add fragment的时候穿进去的值.
            mFragment = (Fragment) fm.findFragmentByTag(tag);
    */
        mBaseFragmentViewPageAdapter = new ViewPagerFragmentAdapter(getContext(), getChildFragmentManager()) {
            @Override
            public void addPage(ArrayList<String> tabTitles, ArrayList<FragmentInfo> fragmentInfos) {
                DepositFragment.this.addPage(tabTitles, fragmentInfos);
            }
        };

        initContainer(mBaseFragmentViewPageAdapter, view);
        setScreenPageLimit();
    }

    public void setScreenPageLimit() {

    }

    protected abstract void addPage(ArrayList<String> tabTitles, ArrayList<FragmentInfo> fragmentInfos);

    public void initContainer(final PagerAdapter pagerAdapter, View view) {
        //mViewPager.setOffscreenPageLimit(limit);// 表示limit个界面之间来回切换都不会重新加载
        if (mViewPager == null) {//这样判断有问题
            mViewPager = (XViewPager) getViewById(R.id.view_pager);
//            mViewPager = (XViewPager) view.findViewById(R.id.view_pager);
        }
        // java.lang.IllegalStateException: Recursive entry to executePendingTransactio
       /* new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mViewPager != null) {
                    mViewPager.setAdapter(pagerAdapter);
                }
            }
        }, 20);*/

     if (mViewPager != null) {
            mViewPager.setAdapter(pagerAdapter);
        }
    }

    public void setCurrentItem(int item) {
        mViewPager.setCurrentItem(item, false);
    }

    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }


    /**
     * 基类会根据不同的catalog展示相应的数据
     *
     * @param catalog 要显示的数据类别
     * @return
     */
    public Bundle getBundle(String catalog) {
        Bundle bundle = new Bundle();
        bundle.putString("11", catalog);
        return bundle;
    }

    public Bundle getBundle(int newType) {
        Bundle bundle = new Bundle();
        bundle.putInt("11", newType);
        return bundle;
    }
}
